package com.scf.controller;

import com.scf.common.exception.BusinessException;
import com.scf.common.vo.DemoInfo;
import com.scf.common.vo.GroupB;
import com.scf.common.vo.UserDTO;
import com.scf.common.vo.ValidationList;
import lombok.extern.slf4j.Slf4j;
import org.junit.platform.commons.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.io.UnsupportedEncodingException;

/**
 * @author sunchuanfu
 */
@Slf4j
@RestController
@RequestMapping("/demo")
public class DemoController {

    @GetMapping("/hello")
    public Object hello(@RequestParam String name) {
        System.out.println("hello");
        return "hello, " + name;
    }

    /**
     * 直接在方法上加验证标签，不需要单独定义vo
     */
    @PostMapping("/test")
    public Object test(@RequestParam(value = "name") @Validated @NotBlank(message = "姓名不能为空") String name) {
        System.out.println("test");
        return "success";
    }

    /**
     * <a href="https://www.cnblogs.com/funyoung/p/8670550.html">...</a>
     * 使用hibernate-validate对参数进行验证
     * (在@Validated修饰的参数后，需要跟上BindingResult参数
     * 没有紧跟BindingResult对象，那么当参数不符合时，将直接返回400 Bad Request状态码)
     * "@Validated(value = {GroupA.class})" 提定检验的分组
     */
    @PostMapping("/saveDemoInfo")
    public Object saveDemoInfo(@RequestBody @Validated(GroupB.class) DemoInfo demoInfo, BindingResult bindingResult) {
        log.info("saveDemoInfo");
        return "success";
    }

    @PostMapping("/saveDemoInfo2")
    public Object saveDemoInfo2(@RequestBody @Validated DemoInfo demoInfo, BindingResult bindingResult) {
        log.info("saveDemoInfo2");
        return "success";
    }

    @PostMapping("/updateAppStatus")
    public Object updateAppStatus(@RequestParam String appId, @RequestParam Integer status,
                                  @RequestParam(required = false) String updateReason) throws UnsupportedEncodingException {
        if (this.checkIsEmpty(appId, status)) {
            throw new BusinessException("参数为空");
        }
        // if (StringUtils.isNotBlank(updateReason)) {
            // updateReason = URLDecoder.decode(updateReason, "UTF-8");// updateReason参数转码
        // }
        return "success";
    }

    /**
     * 集合校验
     * <p>
     *     如果我们直接使用java.util.Collection下的list或者set来接收数据，参数校验并不会生效！-- 其实是可以生效，不过需要加用注解@vaLid
     *     我们可以使用自定义list集合（ValidationList）来接收参数
     * </p>
     */
    @PostMapping("/saveList")
    public Object saveList(@RequestBody @Validated ValidationList<UserDTO> userDTOList, BindingResult bindingResult) {
        return "success";
    }

    private boolean checkIsEmpty(Object... params) {
        for (Object param : params) {
            if (param == null) {
                log.error("param is null!");
                return true;
            }
            if (param instanceof String) {
                if (StringUtils.isBlank(param.toString())) {
                    log.error("param[{}] is null!", param);
                    return true;
                }
            }
        }
        return false;
    }

}

